/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmNodes.java
 *
 * Created on 30 Σεπ 2011, 9:19:40 μμ
 */
package routing;

import routing.Enumerators.TableNames;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 *
 * @author elias
 */
public class FrmNodes extends javax.swing.JFrame {
   
    //Variable Declaration
    JScrollPane NodesTableScrollPanel;
    public static TableModel NodesTm;
    JTable NodesTable;
   // public static TableRowChanged Changed;

    /** Creates new form FrmNodes */
    public FrmNodes() {
        initComponents();
        btnStart.setIcon(new ImageIcon("./Resources/next-icon.png"));
        btnBack.setIcon(new ImageIcon("./Resources/back-icon.png"));
        LoadNodesTable();
    }
  
    
    
    public final void LoadNodesTable(){
   NodesTm = new TableModel(TableNames.Nodes);
   NodesTable = new JTable(NodesTm);
   NodesTable.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                NodesTable.setModel(new TableModel(TableNames.Nodes));
            }
        });
   NodesTableScrollPanel=new JScrollPane(NodesTable);
   NodesTablePanel.setLayout(new FlowLayout());
   NodesTablePanel.add(NodesTableScrollPanel);  
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        NodesTablePanel = new javax.swing.JPanel();
        NodesTableButtons = new javax.swing.JPanel();
        btnDeleteRow = new javax.swing.JButton();
        btnNewRow = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        btnStart = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        NodesTablePanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        getContentPane().add(NodesTablePanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 480, 440));

        btnDeleteRow.setText("Delete Row");
        btnDeleteRow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteRowActionPerformed(evt);
            }
        });

        btnNewRow.setText("New Row");
        btnNewRow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewRowActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout NodesTableButtonsLayout = new javax.swing.GroupLayout(NodesTableButtons);
        NodesTableButtons.setLayout(NodesTableButtonsLayout);
        NodesTableButtonsLayout.setHorizontalGroup(
            NodesTableButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnDeleteRow, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(btnNewRow, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        NodesTableButtonsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnDeleteRow, btnNewRow});

        NodesTableButtonsLayout.setVerticalGroup(
            NodesTableButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(NodesTableButtonsLayout.createSequentialGroup()
                .addComponent(btnDeleteRow)
                .addGap(7, 7, 7)
                .addComponent(btnNewRow))
        );

        NodesTableButtonsLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnDeleteRow, btnNewRow});

        getContentPane().add(NodesTableButtons, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 20, 110, 70));

        jPanel2.setForeground(new java.awt.Color(240, 240, 240));
        jPanel2.setLayout(null);
        jPanel2.add(filler1);
        filler1.setBounds(0, 0, 608, 0);

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 608, 74));

        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });
        getContentPane().add(btnStart, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 450, 60, 40));

        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        getContentPane().add(btnBack, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 450, 60, 40));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNewRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewRowActionPerformed
        
        //Open frmNewRow with Selected Tab
        frmNewRow NewRow=new frmNewRow("Nodes");
        NewRow.setTitle("Add New Row");
        NewRow.setIconImage(Toolkit.getDefaultToolkit().getImage("./Resources/globe.png"));        
        NewRow.setLocation(this.getLocation().x+400,this.getLocation().y);
        NewRow.setVisible(true);
    }//GEN-LAST:event_btnNewRowActionPerformed

private void btnDeleteRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteRowActionPerformed
    DbConnection db=new DbConnection();
    try {
    Connection conn=db.Connect();
    if(NodesTable.getSelectedRow()>=0){
    db.RemoveFromDb(TableNames.Nodes, conn,String.valueOf(NodesTable.getValueAt(NodesTable.getSelectedRow(), 0)));
    NodesTm.fireTableDataChanged();
    }
    if(!conn.isClosed()){
        conn.close();
    }
        
    } catch (SQLException e) {
        e.printStackTrace();
    }
    
}//GEN-LAST:event_btnDeleteRowActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        CreateNodesWeight();
        Frame[] activeframes=Frame.getFrames();
        for (Frame frame:activeframes){
        frame.dispose();
         }
        frmStart Start =new frmStart();
        Start.setLocationRelativeTo(this);
        Start.setTitle("Execution");
        Start.setVisible(true);
    }//GEN-LAST:event_btnStartActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
         this.dispose();
         frmAreas Areas = new frmAreas();
         Areas.setIconImage(Toolkit.getDefaultToolkit().getImage("./Resources/globe.png"));
         Areas.setTitle("Areas Table");
         Areas.setLocationRelativeTo(null);
         Areas.setVisible(true);
    }//GEN-LAST:event_btnBackActionPerformed

    
    public void CreateNodesWeight(){
        DbConnection db=new DbConnection();
        Connection conn=db.Connect();
        try {
            ResultSet rs=db.SelectFromDb(TableNames.Nodes, "Where Area_flag=0", conn);
            while (rs.next()){
                int key=db.ReturnUniqueKey(TableNames.NodesWeight, conn);
                db.AddToDb("INSERT INTO NodesWeight(ID,NodeID,Connected) Values(" +(key+1)+","+rs.getInt("ID")+",0)", conn);
            }
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(FrmNodes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    /**
     * @param args the command line arguments
     */
   
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel NodesTableButtons;
    private javax.swing.JPanel NodesTablePanel;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnDeleteRow;
    private javax.swing.JButton btnNewRow;
    private javax.swing.JButton btnStart;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
